package si.dejan.picflip.views
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Circ;
	
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.system.Capabilities;
	import flash.utils.Timer;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import si.dejan.picflip.ApplicationFacade;
	import si.dejan.picflip.model.PhotoProxy;
	
	public class ImageMediator extends Mediator implements IMediator
	{
		private var startX:Number = 0;
		private var startY:Number = 0;
		
		private var originalX:Number = 0;
		private var originalY:Number = 0;
		
		private var photoProxy:PhotoProxy;
		private var moveTimer:Timer;
		
		/** replace this with current state big */
		private var imageCentered:Boolean = false;
		
		/** true if mouse down was pressed */
		private var mouseDown:Boolean = false;
		
		/** Takes care of a single image component view */
		public function ImageMediator(photoId:String, viewComponent:ImageComponent)
		{
			super(photoId + "Mediator", viewComponent);
			
			photoProxy = facade.retrieveProxy(photoId + "Proxy") as PhotoProxy;
			photoProxy.loadSmallImage();
		}
		
		override public function onRegister():void
		{
			ui.addEventListener(ui.MINIMIZE, handleMinimize);
			ui.addEventListener(ui.LOAD_BIG_IMAGE, loadBigImage);
			
			ui.addEventListener(ui.START_DRAG, handleMouseDown);
			ui.addEventListener(ui.STOP_DRAG, handleMouseUp);
			
		}
		
		override public function onRemove():void
		{
			ui.removeEventListener(ui.MINIMIZE, handleMinimize);
			ui.removeEventListener(ui.LOAD_BIG_IMAGE, loadBigImage);
			
			ui.removeEventListener(ui.START_DRAG, handleMouseDown);
			ui.removeEventListener(ui.STOP_DRAG, handleMouseUp);
		}
		
		override public function listNotificationInterests():Array
		{
			return [photoProxy.loadCompletedEvent];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			switch (notification.getName())
			{
				case photoProxy.loadCompletedEvent :
					if(!imageCentered)
					{
						ui.moveMe.xTo = Math.abs(Math.random() * Capabilities.screenResolutionX - notification.getBody().width);
						ui.moveMe.yTo = Math.abs(Math.random() * Capabilities.screenResolutionY - notification.getBody().height);
						originalX = ui.moveMe.xTo;
						originalY = ui.moveMe.yTo;
					}
					else
					{
						ui.moveMe.xTo = (Capabilities.screenResolutionX / 2) - notification.getBody().width / 2;
						ui.moveMe.yTo = (Capabilities.screenResolutionY / 2) - notification.getBody().height / 2;							
					}
					
					
					ui.moveMe.play();
					/*
					ui.resize.widthTo = uiImage.width;
					ui.resize.heightTo = uiImage.height;
					*/
					// ui.resize.play();
					
					
					ui.newWidth = (notification.getBody() as Bitmap).width;
					ui.newHeight = (notification.getBody() as Bitmap).height;
					
					if(notification.getType() == photoProxy.TYPE_SMALL)
					{
						ui.currentState = "small";
						ui.image.source = notification.getBody();
						
						ui.widthFrom = (notification.getBody() as Bitmap).width;
						ui.heightFrom = (notification.getBody() as Bitmap).height;
					}
					else if(notification.getType() == photoProxy.TYPE_BIG)
					{
						ui.currentState = "big";
						ui.bigImage.source = notification.getBody();
						ui.mouseChildren = true;
					}
					break;
			}
		}
		
		private function handleMinimize(event:Event):void
		{
			ui.currentState = "small";
			imageCentered = false;
			
			ui.moveMe.xFrom = ui.x;
			ui.moveMe.yFrom = ui.y;
					
			ui.moveMe.xTo = originalX;
			ui.moveMe.yTo = originalY;
			
			ui.moveMe.play();
		}
		
		private function handleMouseDown(event:Event):void
		{
			mouseDown = true;
			moveTimer = new Timer(100);
			startX = ui.x;
			startY = ui.y;
			sendNotification(ApplicationFacade.IMAGE_CLICK, ui);
			
			moveTimer.start();
		}
		
		private function handleMouseUp(event:Event):void
		{
			originalX = ui.x;
			originalY = ui.y;
			
			if(!mouseDown)
				trace("SHOULD NOT HAPPEN");
			
			mouseDown = false;
			
			moveTimer.stop();
			var stageX:Number = ui.x;
			var stageY:Number = ui.y;
			
			var moveToX:Number = ui.x;
			var moveToY:Number = ui.y;
			
			var moveFactor:Number = 1;
			
			if(moveTimer.currentCount < 4)
				moveFactor = 2;
			
			if(startX < stageX)
				moveToX += 50 * moveFactor;
			else if(startX > stageX)
				moveToX += -50 * moveFactor;
			
			if(startY < stageY)
				moveToY += 50 * moveFactor;
			else if(startY > stageY)
				moveToY += -50 * moveFactor;
			
			if(moveTimer.currentCount < 8) // dont do move effect if user holds the picture for more then 700ms?
				TweenLite.to(ui, 1, {x:moveToX, y:moveToY});
		}
		
		
		private function loadBigImage(event:Event):void
		{
			var xTo:Number = (Capabilities.screenResolutionX / 2) - ui.width / 2;
			var yTo:Number = (Capabilities.screenResolutionY / 2) - ui.height / 2;
			
			TweenLite.to(ui, 1, {x:xTo, y:yTo, ease:Circ.easeInOut, onComplete:completeCenter});
						
			photoProxy.loadBigImage();
		}
		
		private function completeCenter():void
		{
			// ui.currentState = "big";
			imageCentered = true;
			ui.progressBar.source = photoProxy.getLoader().contentLoaderInfo;
		}
		
		private function get ui():ImageComponent
		{
			return viewComponent as ImageComponent;
		}
	}
}